﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
using Model.Entities;
using log4net;

namespace CardDownloader
{
    public partial class FormDownloadCard : Form
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(FormDownloadCard));
        private event EventHandler<EventArgs> OnDownloadFinish;

        public string Url
        {
            get { return txtUrl.Text; }
            set { txtUrl.Text = value; }
        }

        public CardInformation CardInformation { get; set; }

        public FormDownloadCard()
        {
            InitializeComponent();
            OnDownloadFinish += FormMain_OnDownloadFinish;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                lbMessage.Text = "Downloading ...";
                Task.Factory.StartNew(() => DownloadCard(Url));
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        void FormMain_OnDownloadFinish(object sender, EventArgs e)
        {
            try
            {
                if (InvokeRequired)
                    BeginInvoke(new MethodInvoker(ProcessOnDownloadFinish));
                else
                    ProcessOnDownloadFinish();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void ProcessOnDownloadFinish()
        {
            try
            {
                txtCardName.Text = CardInformation.CardName;
                txtLevel.Text = string.Format("{0}/{1}", CardInformation.Level, CardInformation.Rank);
                txtAttribute.Text = CardInformation.Attribute;
                txtTypes.Text = ArrayToString(CardInformation.Types);
                txtCardNumber.Text = CardInformation.CardNumber;
                txtATKDEF.Text = string.Format("{0}/{1}", CardInformation.Atk.HasValue ? CardInformation.Atk.Value.ToString() : "?", CardInformation.Def.HasValue ? CardInformation.Def.Value.ToString() : "?");
                StringBuilder builder = new StringBuilder();
                builder.AppendFormat("- CardEffectTypes: {0}", ArrayToString(CardInformation.CardEffectTypes));
                builder.AppendLine();
                builder.Append("- CardSearchCategories:");
                builder.AppendLine();
                foreach (KeyValuePair<string, string[]> cardSearchCategory in CardInformation.CardSearchCategories)
                {
                    builder.AppendFormat("{0}: {1}", cardSearchCategory.Key, ArrayToString(cardSearchCategory.Value));
                    builder.AppendLine();
                }
                builder.AppendFormat("- CardNumber: {0}", CardInformation.CardNumber);
                builder.AppendLine();
                builder.AppendFormat("- Property: {0}", CardInformation.Property);
                builder.AppendLine();
                builder.AppendFormat("- Url: {0}", CardInformation.Url);
                builder.AppendLine();
                builder.AppendFormat("- ImageUrl: {0}", CardInformation.ImageUrl);
                builder.AppendLine();
                builder.AppendFormat("- ImageThumbnailUrl: {0}", CardInformation.ImageThumbnailUrl);
                builder.AppendLine();
                builder.AppendFormat("- CardDescription: {0}", CardInformation.CardDescription);
                txtDescription.Text = builder.ToString();

                lbMessage.Text = "Download Finish";
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private string ArrayToString(string[] strings)
        {
            try
            {
                string result = string.Empty;
                for (int i = 0; i < strings.Length - 1; i++)
                {
                    result += strings[i] + ", ";
                }
                result = result + strings[strings.Length - 1];
                return result;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            return string.Empty;
        }

        private void DownloadCard(string url)
        {
            try
            {
                CardInformation = Utils.DownloadCard(url);
                if (OnDownloadFinish != null)
                    OnDownloadFinish(this, new EventArgs());
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
    }
}
